package com.example.Sao.Tools;

import android.annotation.SuppressLint;
import android.os.Build;
import android.util.Log;

import java.lang.reflect.Method;

/**
 * @ProjectName: scan
 * @Package: com.example.MyApplication.Tools
 * @ClassName: DeviceSNUntil
 * @Description: java类作用描述
 * @Author: 作者名
 * @CreateDate: 2020/4/22 17:59
 * @UpdateUser: 更新者：
 * @UpdateDate: 2020/4/22 17:59
 * @UpdateRemark: 更新说明：
 * @Version: 1.0
 */
public class DeviceSNUtil {

    /**
     * 获取手机序列号
     *
     * @return 手机序列号
     */
    @SuppressLint({"NewApi", "MissingPermission"})
    public static String getSerialNumber() {
        String serial = "";
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {//9.0+
                serial = Build.getSerial();
            } else if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N) {//8.0+
                serial = Build.SERIAL;
            } else {//8.0-
                Class<?> c = Class.forName("android.os.SystemProperties");
                Method get = c.getMethod("get", String.class);
                serial = (String) get.invoke(c, "ro.serialno");
            }
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("e", "读取设备序列号异常：" + e.toString());
        }
        return serial;
    }

    /**替换序列号后四位
     * @return
     */
    public static String encryp(String sn){
        StringBuilder sb = new StringBuilder(sn);
        return sb.replace((sn.length()-4),sn.length(),"****").toString();
    }
}
